import { fileURLToPath, URL } from 'node:url'
import { loadEnv } from 'vite'

// 插件
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'

// https://vitejs.dev/config/
export default ({ command, mode }) => {
  const root = process.cwd()
  const env = loadEnv(mode, root)

  return {
    base: env.VITE_APP_BASE + '/',

    build: {
      outDir: `dist/gather`,
    },

    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router'],
      }),
    ],

    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },

    // 代理请求
    server: {
      port: 3500,
      proxy: {
        [env.VITE_APP_API]: {
          target: env.VITE_APP_SERVER,
          changeOrigin: true,
          // rewrite: path => path.replace(/^\/api/, '')
        },
      },
    },
  }
}
